<template>
    <div class="row">
        <img v-show="loading" src="../img/bb117c6c0862da6663b1a1fe695cc6cc.gif"  />
        <div v-show="!loading">
            <div class="card" v-for="user in users" :key="user.id">
                <a :href="user.html_url" target="_blank">
                    <img :src="user.avatar_url" style='width: 100px' />
                </a>
                <p class="card-text">{{ user.login }}</p>
            </div>
        </div>


    </div>
</template>
<script>
import axios from "axios";

export default {
    name: "UserList",
    data() { 
        return {
            loading:false,
            users:[]
        }
    },
    methods: {
        async search(searchName) { 
            console.log("请求进来了");
            try {
                this.loading = true;
                const result = await axios({
                    method: 'get',
                    url: "/api/search/users",
                    params: {
                        q: searchName
                    }
                });
                this.users = result.data.items;
                console.log(this.users);
            } catch (error) {
                console.log(error);
            } finally { 
                this.loading = false;
            }
            
        }
    },
    mounted() {
        this.$bus.$on("search", this.search);
    },
    beforeDestroy() {
        this.$bus.$off("search", this.search);
    }
}
</script>
<style>
.album {
    min-height: 50rem;
    /* Can be removed; just added for demo purposes */
    padding-top: 3rem;
    padding-bottom: 3rem;
    background-color: #f7f7f7;
}

.card {
    float: left;
    width: 33.333%;
    padding: .75rem;
    margin-bottom: 2rem;
    border: 1px solid #efefef;
    text-align: center;
}

.card>img {
    margin-bottom: .75rem;
    border-radius: 100px;
}

.card-text {
    font-size: 85%;
}


</style>